package com.ydgk.dao;

import com.ydgk.bean.Book;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

public class BookDao extends BaseDao<Book> {
    @Override
    public int save(Book book) {
        return template.update("insert into book values(?,?,?,?,?,?)",
                book.getBid(),
                book.getBname(),
                book.getBauthor(),
                book.getBprice(),
                book.getBdate(),
                book.getSid());
    }

    @Override
    public int updateById(Book book) {
        return template.update("update book set bname = ?,bauthor = ?,bprice = ?,bdate = ?,sid = ? where bid = ?",
                book.getBname(),
                book.getBauthor(),
                book.getBprice(),
                book.getBdate(),
                book.getSid(),
                book.getBid());
    }

    @Override
    public int deleteById(int id) {
        return template.update("delete from book where bid = ?" , id);
    }

    @Override
    public List findAll() {
        return template.query("select * from book",new BeanPropertyRowMapper<Book>(Book.class));
    }

    @Override
    public Book findById(int id) {
        String sql = "select * from book where bid =?" ;
        List<Book> list = template.query(sql, new BeanPropertyRowMapper<Book>(Book.class),id);
        return list.size() > 0 ? list.get(0) : null ;
    }

    @Override
    public List paging(int pageNumber) {
        return template.query("select * from book order by bid desc limit ?,?",new BeanPropertyRowMapper<Book>(Book.class) , (pageNumber-1)*pageCount ,pageCount);
    }

    public int findAllNumber(){
        String sql = "select count(*) from book";
        long t = template.queryForObject(sql, Long.class);
        int total = (int)t;//将long类型转换成int类型
        return total;
    }

}
